userNotify = (function ($) {
    var _onSearch = false;
    var pub = {
        notifyId: undefined,
        assignUrl: undefined,
        searchUrl: undefined,
        assign: function (action) {
            var params = {
                id: pub.notifyId,
                action: action,
                users: $('#list-' + (action == 'assign' ? 'avaliable' : 'assigned')).val(),
            };
            $.post(pub.assignUrl, params,
                function (result) {
                    if (result.errorCode == 0) {
                        pub.searchUser('avaliable', true);
                        pub.searchUser('assigned', true);
                    } else {
                        alert(result.message.message);
                    }
                });
        },
        searchUser: function (target, force) {
            if (!_onSearch || force) {
                _onSearch = true;
                var $inp = $('#search-' + target);
                setTimeout(function () {
                    var data = {
                        id: pub.notifyId,
                        target: target,
                        term: $inp.val(),
                    };
                    $.get(pub.searchUrl, data,
                        function (r) {
                            var $list = $('#list-' + target);
                            $list.html('');
                            if (r.Dealers) {
                                var $group = $('<optgroup label="经销商">');
                                $.each(r.Dealers, function (key, value) {
                                    $('<option>').val(value.id + '|dealer').text(value.company_name).appendTo($group);
                                });
                                $group.appendTo($list);
                            }
                            if (r.Stores) {
                                var $group = $('<optgroup label="连锁店">');
                                $.each(r.Stores, function (key, value) {
                                    $('<option>').val(value.id + '|federation_store').text(value.company_name).appendTo($group);
                                });
                                $group.appendTo($list);
                            }
                        }).done(function () {
                        _onSearch = false;
                    });
                }, 500);
            }
        },
        initProperties: function (properties) {
            $.each(properties, function (key, val) {
                pub[key] = val;
            });
        },
    }
    return pub;
})(jQuery)